<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格类型" prop="shopName">
        <el-input
          v-model="queryParams.specificationName"
          placeholder="请输入规格类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格名称" prop="flavourName">
        <el-input
          v-model="queryParams.flavourName"
          placeholder="请输入规格名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orderEat:flavour:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-connection"
          size="mini"
          @click="handleAssociation"
          v-hasPermi="['orderEat:flavour:add']"
        >关联
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['orderEat:flavour:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['orderEat:flavour:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flavourList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="shopName"/>
      <el-table-column label="规格类型名称" align="center" prop="specificationName"/>
      <el-table-column label="规格名称" align="center" prop="flavourName"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['orderEat:flavour:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['orderEat:flavour:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加规格对话框 -->
    <el-dialog :title="title" :visible.sync="open" :before-close="handleClose" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商品名称" prop="shopId">
          <el-select v-model="form.shopId" placeholder="请选择">
            <el-option
              v-for="item in shopinfoList"
              :key="item.shopId"
              :label="item.shopName"
              :value="item.shopId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showSku" label="商品规格类型" prop="specificationId">
          <el-select v-model="form.specificationId" placeholder="请选择">
            <el-option
              v-for="item in shopSpecificationList"
              :key="item.specificationId"
              :label="item.specificationName"
              :value="item.specificationId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="showInput" label="商品规格类型" prop="specificationName">
          <el-input v-model="form.specificationName" placeholder="请输入规格类型"/>
        </el-form-item>
        <el-form-item v-if="showFlavour" label="规格名称" prop="flavourName">
          <el-input v-model="form.flavourName" placeholder="请输入规格名称"/>
        </el-form-item>

      </el-form>
      <div v-if="showNext" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toNext">下一步</el-button>
        <el-button @click="toJump">跳过</el-button>
      </div>

      <div v-if="showFlavour" slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改规格对话框 -->
    <el-dialog :title="title" :visible.sync="updateOpen" :before-close="handleClose" width="500px" append-to-body>
      <el-form ref="form" :model="updateform"  label-width="100px">
        <el-form-item label="商品名称" prop="shopId">
          <el-input v-model="updateform.shopName" disabled="true"/>
        </el-form-item>
        <el-form-item label="商品规格类型" prop="specificationId">
          <el-select v-model="updateform.specificationId" placeholder="请选择" @change="getSpecificationId(updateform.specificationId)">
            <el-option
              v-for="item in shopSpecificationList"
              :key="item.specificationId"
              :label="item.specificationName"
              :value="item.specificationId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格名称" prop="flavourName">
          <el-select  v-model="updateform.flavourId"  placeholder="请选择">
            <el-option
              v-for="item in flavourDropList"
              :key="item.flavourId"
              :label="item.flavourName"
              :value="item.flavourId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-loading="loading" @click="conformUpdate">确 定</el-button>
<!--        <el-button type="primary"  @click="conformUpdate">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 关联规格对话框 -->
    <el-dialog title="关联商品规格" :visible.sync="associationOpen" :before-close="associationClose" width="500px"
               append-to-body
    >
      <el-form ref="form" :model="associationForm" :rules="rules" label-width="100px">
        <el-form-item label="商品名称">
          <el-select v-model="associationForm.shopId" placeholder="请选择">
            <el-option
              v-for="item in shopinfoList"
              :key="item.shopId"
              :label="item.shopName"
              :value="item.shopId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择规格类型">
          <el-select v-model="associationForm.specificationId" placeholder="请选择" @change="getSpecificationId(associationForm.specificationId)">
            <el-option
              v-for="item in shopSpecificationList"
              :key="item.specificationId"
              :label="item.specificationName"
              :value="item.specificationId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择规格名称">
          <el-select v-model="associationForm.flavourIds" multiple :disabled = flavourDisable placeholder="请选择">
            <el-option
              v-for="item in flavourDropList"
              :key="item.flavourId"
              :label="item.flavourName"
              :value="item.flavourId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAssociationForm">确 定</el-button>
        <el-button @click="associationCancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listFlavour,
  listshopSpecification,
  echoFlavour,
  updateFlavour,
  delFlavour,
  addFlavour,
  droplistFlavour,
  associationFlavour
} from '@/api/orderEat/flavour'
import { dropDownList } from '@/api/orderEat/shopInfo'

export default {
  name: 'Flavour',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品口味表格数据
      flavourList: [],
      // 弹出层标题
      title: '',
      // 是否显示新增弹出层
      open: false,
      //是否显示修改弹出层
      updateOpen:false,
      // 是否显示关联弹出层
      associationOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flavourName: null
      },
      // 表单参数
      form: {},
      //修改表单参数
      updateform:{},
      // 关联表单参数
      associationForm: {},
      //商品下拉列表
      shopinfoList: {},
      //商品规格类型下拉列表
      shopSpecificationList: {},
      //商品规格名称下拉列表
      flavourDropList: {},
      //新增规格判断1
      showSku: true,
      //新增规格判断1
      showInput: false,
      //新增规格判断2
      showFlavour: false,
      //新增规格下一步判断
      showNext: true,
      //用于选择的规格ID
      specificationId:0,
      //关联规格名称选择
      flavourDisable:true,
      //修改规格名称选择
      upFlavourDisable:true,
      //表单验证规则
      rules: {
        shopId: [
          { required: true, message: '请选择商品', trigger: 'change' }
        ],
        flavourName: [
          { required: true, message: '请输入规格名称', trigger: 'blur' }
        ]
      }

    }

  },
  created() {
    this.getList()
    this.getshopList()
    this.getShopSpecificationList()
  },
  methods: {
    /** 查询商品口味列表 */
    getList() {
      this.loading = true
      listFlavour(this.queryParams).then(response => {
        this.flavourList = response.rows
        this.total = response.total
        this.loading = false
        this.showSku = true
        this.showInput = false
        this.showNext = true
        this.showFlavour = false
        this.flavourDisable = true
        this.specificationId = 0
      })
    },
    /** 查询商品下拉列表 */
    getshopList() {
      this.loading = true
      dropDownList().then(response => {
        this.shopinfoList = response.data
        this.loading = false
      })
    },
    /** 查询商品规格类型下拉列表 */
    getShopSpecificationList() {
      this.loading = true
      listshopSpecification().then(response => {
        this.shopSpecificationList = response.data
        this.loading = false
      })
    },
    getSpecificationId(param){
      this.flavourDisable = false;
      this.upFlavourDisable = false;
      this.updateform.flavourId = null;
      this.getFlavourList(param);
    },
    /** 查询商品规格类型下拉列表 */
    getFlavourList(param){
      droplistFlavour(param).then(response =>{
        this.flavourDropList = response.data
      })
    },
    /** 下一步 */
    toNext() {
      this.showNext = false
      this.showFlavour = true
    },
    /** 跳过 */
    toJump() {
      this.showNext = false
      this.showFlavour = true
      this.showInput = true
      this.showSku = false
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.showSku = true
      this.showInput = false
      this.showNext = true
      this.showFlavour = false
      this.updateOpen = false
    },
    // 关联取消按钮
    associationCancel() {
     this.associationOpen = false;
    },
    // 关闭按钮
    handleClose() {
      this.cancel()
    },
    // 关联窗口关闭按钮
    associationClose(){
      this.associationCancel();
    },
    // 表单重置
    reset() {
      this.form = {
        flavourId: null,
        shopId: null,
        flavourSign: null,
        flavourName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    // 表单重置
    associationReset() {
      this.associationForm = {
      }

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品规格'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //this.reset()
      let shopFlavour = {
        'shopId':row.shopId,
        'specificationId':row.specificationId,
        'flavourId':row.flavourId,
      }
      echoFlavour(shopFlavour).then(response=>{
        this.updateOpen = true
        this.updateform = response.data
        this.getFlavourList(this.updateform.specificationId);
        this.title = '修改商品口味'
      })
    },
    /** 确认修改按钮 */
    conformUpdate(){
      this.loading = true;
      updateFlavour(this.updateform).then(response=>{
        this.$modal.msgSuccess('修改成功')
        //this.updateform = null;
        this.loading = false;
        this.getList();
      })
      this.updateOpen = false;

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addFlavour(this.form).then(response => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
            this.getShopSpecificationList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const idt =[];
      if (row.id!=null){
        idt.push(row.id);
      }else {
        this.ids.forEach(id=>{
            idt.push(id)
          })
      }
      let delDate = {'ids':idt};
      this.$modal.confirm('是否确认删除商品').then(function() {
        return delFlavour(delDate)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 关联确认按钮 */
    submitAssociationForm(){
      associationFlavour(this.associationForm).then(response=>{
        this.associationOpen=false;
        this.getList();
        this.associationReset();
        this.flavourDisable = true;
      })
    },
    /** 关联按钮操作 */
    handleAssociation() {
      this.associationOpen = true
      this.associationReset()
      this.flavourDisable = true
    }
  }
}
</script>
